@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>编辑外协发料单</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" />
    <style>
        body { padding: 20px; background: #fff; }
        .layui-form { max-width: 100%; }
        .layui-form-item { margin-bottom: 15px; }
        .required { color: red; }
    </style>
</head>
<body>
    <form class="layui-form" id="editForm" lay-filter="editForm">
        <input type="hidden" name="id" id="hiddenId">
        
        <div class="layui-row layui-col-space16">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="required">*</span>发料单编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="issueListCode" placeholder="请输入发料单编号" class="layui-input" lay-verify="required">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="required">*</span>发料单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="issueListName" placeholder="请输入发料单名称" class="layui-input" lay-verify="required">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row layui-col-space16">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">发料日期</label>
                    <div class="layui-input-block">
                        <input type="text" name="issueDate" placeholder="请选择发料日期" class="layui-input" id="issueDate">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">发料仓库</label>
                    <div class="layui-input-block">
                        <select name="issueWarehouse" lay-filter="issueWarehouse" lay-search>
                            <option value="">请选择</option>
                            <option value="仓库1">仓库1</option>
                            <option value="仓库2">仓库2</option>
                            <option value="仓库3">仓库3</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row layui-col-space16">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">单据状态</label>
                    <div class="layui-input-block">
                        <select name="state" lay-filter="state">
                            <option value="">请选择</option>
                            <option value="0">待审核</option>
                            <option value="1">已完成</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">外协工单</label>
                    <div class="layui-input-block">
                        <div class="layui-input-wrap">
                            <input type="text" name="outsourcedOrders" placeholder="请选择外协工单" class="layui-input" readonly>
                            <div class="layui-input-suffix">
                                <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" onclick="selectWorkOrder()">
                                    <i class="layui-icon layui-icon-search"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row layui-col-space16">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">供应商</label>
                    <div class="layui-input-block">
                        <div class="layui-input-wrap">
                            <input type="text" name="vendor" placeholder="请选择供应商" class="layui-input" readonly>
                            <div class="layui-input-suffix">
                                <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" onclick="selectVendor()">
                                    <i class="layui-icon layui-icon-search"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">供应商名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="vendorName" placeholder="请选择外协工单" class="layui-input" readonly>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" class="layui-textarea" rows="3"></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center; margin-top: 30px;">
            <button type="submit" class="layui-btn" lay-submit lay-filter="submitForm">保存</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="closeDialog()">取消</button>
        </div>
    </form>

    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'layer'], function () {
            var form = layui.form;
            var laydate = layui.laydate;
            var layer = layui.layer;

            // 日期选择器
            laydate.render({
                elem: '#issueDate'
            });

            // 获取URL参数中的ID
            function getUrlParameter(name) {
                name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
                var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
                var results = regex.exec(location.search);
                return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
            }

            // 加载数据
            var id = getUrlParameter('id');
            if (id) {
                $('#hiddenId').val(id);
                loadData(id);
            }

            function loadData(id) {
                $.ajax({
                    url: '/Outsourcing/FanOutsourcingMaterial',
                    type: 'GET',
                    data: { outsourcingMaterialId: id },
                    success: function (data) {
                        if (data) {
                            // 填充表单数据
                            form.val('editForm', {
                                'issueListCode': data.issueListCode,
                                'issueListName': data.issueListName,
                                'issueDate': data.issueDate ? layui.util.toDateString(new Date(data.issueDate), 'yyyy-MM-dd') : '',
                                'issueWarehouse': data.issueWarehouse,
                                'state': data.state,
                                'outsourcedOrders': data.outsourcedOrders,
                                'vendor': data.vendor,
                                'vendorName': data.vendorName,
                                'remark': data.remark
                            });
                        }
                    },
                    error: function () {
                        layer.msg('加载数据失败', { icon: 2 });
                    }
                });
            }

            // 表单提交
            form.on('submit(submitForm)', function (data) {
                var formData = data.field;
                
                // 日期格式处理
                if (formData.issueDate) {
                    formData.issueDate = new Date(formData.issueDate).toISOString();
                }

                $.ajax({
                    url: '/Outsourcing/UpdateOutsourcingMaterial',
                    type: 'POST',
                    data: formData,
                    success: function (result) {
                        if (result > 0) {
                            layer.msg('保存成功', { icon: 1 }, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg('保存失败', { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg('保存失败', { icon: 2 });
                    }
                });
                return false;
            });
        });

        // 选择工单
        function selectWorkOrder() {
            layer.msg('选择工单功能待实现');
            // 这里可以打开工单选择弹框
        }

        // 选择供应商
        function selectVendor() {
            layer.msg('选择供应商功能待实现');
            // 这里可以打开供应商选择弹框
        }

        // 关闭弹框
        function closeDialog() {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    </script>
</body>
</html> 